package com.lxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.common.R;
import com.lxp.entity.Employee;
import com.lxp.service.EmployeeService;
import com.lxp.utlis.BaseContext;
import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author lxp
 * @Motto 每天一行小代码，健康幸福学JAVA：
 * @create 2022-08-22 19:05
 * @Description:
 */


@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录功能
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        // 1、将页面提交的密码password进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        // 2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);
        // 3、如果没有查询到则返回登录失败结果
        if (emp == null){
            return R.error("账号输入错误，请重新输入！");
        }
        // 4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(employee.getPassword())){
            return R.error("密码输入错误，请重新输入！");
        }
        // 5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0){
            return R.error("员工已禁用");
        }
        // 6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }


    /**
     * 员工退出功能
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工信息
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        // 设置员工初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 设置员工的创建时间
        //  employee.setCreateTime(LocalDateTime.now());
        // 设置员工的更新时间
        // employee.setUpdateTime(LocalDateTime.now());

        // 获取当前登录用户的id
        // Long empId = (Long) request.getSession().getAttribute("employee");
        // 设置是 谁 添加的员工信息
        // employee.setCreateUser(empId);
        // 设置是 谁 更新的员工信息
        // employee.setUpdateUser(empId);

        boolean save = employeeService.save(employee);
        if (save){
            return R.success("添加员工信息成功");
        }
        return R.error("添加员工信息失败");
    }


    /**
     *  分页查询数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(Integer page,Integer pageSize,String name){

        // 构造分页构造器
        IPage empList = new Page(page,pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加条件过滤
        lqw.like(Strings.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);

        // 执行查询
        employeeService.page(empList, lqw);

        return R.success(empList);
    }

    /**
     * 根据ID修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee){
        // Long empId = (Long) request.getSession().getAttribute("employee");

        // 设置修改的时间
        // employee.setUpdateTime(LocalDateTime.now());
        // 设置是谁进行的修改
        // employee.setUpdateUser(empId);

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据学ID查询查询信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到相关的员工信息");

    }


}
